package com.xiaobai.SynchrobizedLock;


import lombok.extern.slf4j.Slf4j;
import org.openjdk.jol.info.ClassLayout;

import java.util.Vector;
import java.util.concurrent.locks.LockSupport;

@Slf4j(topic = "c.TestBiased2")
public class TestBiased2 {
    //-XX:BiasedLockingStartupDelay=0 -XX:-TieredCompilation -XX:+UseBiasedLocking
    static Thread t1,t2,t3;

    public static void main(String[] args) throws InterruptedException {
        test4();
    }
    private static void test4() throws InterruptedException {
        Vector<Dog> list = new Vector<>();

        int loopNumber = 38;
        t1 = new Thread(() -> {
            for (int i = 0; i < loopNumber; i++) {
                Dog d = new Dog();
                list.add(d);
                synchronized (d) {
                    log.debug("{}\t{}", i, ClassLayout.parseInstance(d).toPrintable());
                }
            }
            LockSupport.unpark(t2);
        }, "t1");
        t1.start();

        t2 = new Thread(() -> {
            LockSupport.park();
            log.debug("===============> ");
            for (int i = 0; i < loopNumber; i++) {
                Dog d = list.get(i);
                log.debug("{}\t{}", i, ClassLayout.parseInstance(d).toPrintable());
                synchronized (d) {
                    log.debug("{}\t{}", i, ClassLayout.parseInstance(d).toPrintable());
                }
                log.debug("{}\t{}", i, ClassLayout.parseInstance(d).toPrintable());
            }
            LockSupport.unpark(t3);
        }, "t2");
        t2.start();

        t3 = new Thread(() -> {
            LockSupport.park();
            log.debug("===============> ");
            for (int i = 0; i < loopNumber; i++) {
                Dog d = list.get(i);
                log.debug("{}\t{}", i, ClassLayout.parseInstance(d).toPrintable());
                synchronized (d) {
                    log.debug("{}\t{}", i, ClassLayout.parseInstance(d).toPrintable());
                }
                log.debug("{}\t{}", i, ClassLayout.parseInstance(d).toPrintable());
            }
        }, "t3");
        t3.start();

        t3.join();
        //超过了40的阈值就不会再进行偏向了
        log.debug("new object \n {}",ClassLayout.parseInstance(new Dog()).toPrintable());
    }
}



